package com.kanshu.ksgb.fastread.doudou.module.bookcity.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dl7.recycler.divider.VerticalDividerItemDecoration;
import com.dl7.recycler.helper.RecyclerViewHelper;
import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.constants.ARouterConfig;
import com.kanshu.ksgb.fastread.doudou.app.constants.Constants;
import com.kanshu.ksgb.fastread.doudou.common.util.ARouterUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.DisplayUtils;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.adapter.StyleFourAdapter;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.bean.ItemRefreshBean;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.bean.SelectedBean;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 人气专区
 * Created by wcy on 18/6/4.
 */
public class AdSelectedFour4Layout extends LinearLayout implements IRefresh<SelectedBean> {
    @BindView(R.id.label)
    TextView mLabel;
    @BindView(R.id.container)
    RecyclerView mContainer;
    Unbinder mUnbinder;
    ItemRefreshBean mItemRefreshBean;
    @BindView(R.id.sub_title)
    TextView mSubTitle;
    @BindView(R.id.more)
    TextView mMore;
    RecyclerView.ItemDecoration mItemDecoration;
    private String mType;
    private RecyclerView.Adapter mAdapter;
    public AdSelectedFour4Layout(Context context) {
        super(context);
        init();
    }

    public AdSelectedFour4Layout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public AdSelectedFour4Layout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    private void init() {
        setOrientation(VERTICAL);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.item_ad_selected_four_layout, this);
        mUnbinder = ButterKnife.bind(view);
    }

    @Override
    public void refresh(SelectedBean selectedBean, Object object) {
        if (selectedBean == null || object == null) {
            return;
        }
        mItemRefreshBean = (ItemRefreshBean) object;
        mLabel.setText(mItemRefreshBean.selectionName);
        mLabel.setCompoundDrawablesWithIntrinsicBounds(mItemRefreshBean.selectionIcon, 0, 0, 0);
        if (TextUtils.isEmpty(mItemRefreshBean.subTitle)) {
            DisplayUtils.gone(mSubTitle);
        } else {
            DisplayUtils.visible(mSubTitle);
            mSubTitle.setText(mItemRefreshBean.subTitle);
        }
        inVisibleMoreEntry(mItemRefreshBean.type);
        String type = mItemRefreshBean.type;
        mType = type;
        if (mAdapter == null) {
            mLabel.setCompoundDrawablesWithIntrinsicBounds(mItemRefreshBean.selectionIcon, 0, 0, 0);
            if (mItemDecoration != null) {
                mContainer.removeItemDecoration(mItemDecoration);
            }
            switch (type) {
                case Constants.BookCityModuleType.TYPE_MALE_RENQIZHUANQU:
                case Constants.BookCityModuleType.TYPE_FEMALE_RENQI:
                    mItemDecoration = new VerticalDividerItemDecoration.Builder(getContext()).size(DisplayUtils.dip2px(getContext(), 20)).color(getResources().getColor(R.color.transparent)).build();
                    mContainer.addItemDecoration(mItemDecoration);
                    mAdapter = new StyleFourAdapter(getContext(), selectedBean.list, mType);
                    break;
                default:
                    mItemDecoration = new VerticalDividerItemDecoration.Builder(getContext()).size(DisplayUtils.dip2px(getContext(), 20)).color(getResources().getColor(R.color.transparent)).build();
                    mContainer.addItemDecoration(mItemDecoration);
                    mAdapter = new StyleFourAdapter(getContext(), selectedBean.list, mType);
                    break;
            }
            RecyclerViewHelper.initRecyclerViewH(getContext(), mContainer, mAdapter);
        }

    }

    @Override
    public void onError(int code, String desc) {

    }

    @Override
    public void reset() {
        mAdapter = null;
    }

    @OnClick(R.id.more)
    public void onViewClicked() {
        Map<String, Object> map = new HashMap<>();
        map.put("tab_pos", 0);
        // 跳转更多页面
        if (Constants.BookCityModuleType.TYPE_MALE_RENQIZHUANQU.equals(mType)) {
            map.put("site", Constants.MALE);
        } else if(Constants.BookCityModuleType.TYPE_FEMALE_RENQI.equals(mType)) {
            map.put("site", Constants.FEMALE);
        }
        ARouterUtils.toActivity(ARouterConfig.RANKING_LIST, map);
    }

    private void inVisibleMoreEntry(String type) {
        DisplayUtils.visible(mMore);
    }
}
